#include "staticchecker.h"

StaticChecker::StaticChecker()
{
    b_cpu_userSys_limit     = false;
    b_cpu_iowait_limit      = false;
    b_cpu_idleRun_limit     = false;
    b_cpu_intrUsed_limit    = false;
    b_cpu_contxtSys_limit   = false;
    b_mem_swapinOut_limit   = false;
    b_mem_leak_limit        = false;

    S_cpu_userSys_up        = 0.6;
    S_cpu_userSys_down      = 0.85;
    S_cpu_iowait_up         = 0.0;
    S_cpu_iowait_down       = 0.25;
    S_cpu_idleRun_iup       = 0.4;
    S_cpu_idleRun_idown     = 1.0;
    S_cpu_idleRun_rup       = 0.0;
    S_cpu_idleRun_rdown     = 4.0;
    S_cpu_intrUsed_iup      = 0.0;
    S_cpu_intrUsed_idown    = 0.15;
    S_cpu_intrUsed_uup      = 0.0;
    S_cpu_intrUsed_udown    = 0.85;
    S_cpu_contxtSys_cup     = 0.0;
    S_cpu_contxtSys_cdown   = 5000.0;
    S_cpu_contxtSys_sup     = 0.0;
    S_cpu_contxtSys_sdown   = 0.4;
    S_mem_swapinOut_in      = 0.0;
    S_mem_swapinOut_out     = 0.0;
    S_mem_leak_uup          = 0.0;
    S_mem_leak_udown        = 0.8;
    S_mem_leak_aup          = 0.0;
    S_mem_leak_adown        = 0.5;
}


bool StaticChecker::CmprS_cpu_userSys(double cpu_user, double cpu_sys, double cpu_total){
    double value = (cpu_total - cpu_user) / cpu_total +
                   (cpu_total - cpu_sys) / cpu_total;
    bool flag = false;

    if((S_cpu_userSys_up > value) || (S_cpu_userSys_down < value)){
        flag = true;
    }

    return flag;
}

bool StaticChecker::CmprS_cpu_iowait(double cpu_iowait, double cpu_total){
    double value = cpu_iowait / cpu_total;
    bool flag = false;

    if((S_cpu_iowait_up > value) || (S_cpu_iowait_down < value)){
        flag = true;
    }

    return flag;
}

bool StaticChecker::CmprS_cpu_idleRun(double cpu_idle, double cpu_total, double cpu_running){
    double value = (cpu_total - cpu_idle) / cpu_total;
    bool flag = false;

    if(((S_cpu_idleRun_iup > value) || (S_cpu_idleRun_idown < value)) ||
       ((S_cpu_idleRun_rup > cpu_running) || (S_cpu_idleRun_rdown < cpu_running))){
        flag = true;
    }

    return flag;
}

bool StaticChecker::CmprS_cpu_intrUsed(double cpu_intr, double cpu_total, double cpu_used){
    double value = cpu_intr / cpu_total;
    bool flag = false;

    if(((S_cpu_intrUsed_iup > value) || (S_cpu_intrUsed_idown < value)) ||
       ((S_cpu_intrUsed_uup > cpu_used) || (S_cpu_intrUsed_udown < cpu_used))){
        flag = true;
    }

    return flag;
}

bool StaticChecker::CmprS_cpu_contxtSys(double cpu_contxt, double cpu_total, double cpu_sys){
    double value = cpu_contxt / cpu_total;
    double vu = cpu_sys / cpu_total;
    bool flag = false;

    if(((S_cpu_contxtSys_cup > value) || (S_cpu_contxtSys_cdown < value)) ||
       ((S_cpu_contxtSys_sup > vu) || (S_cpu_contxtSys_sdown < vu))){
        flag = true;
    }

    return flag;
}

bool StaticChecker::CmprS_mem_swapinOut(double in, double out){
    bool flag = false;

    if((0 < in) || (0 < out)){
        flag = true;
    }

    return flag;
}

bool StaticChecker::CmprS_mem_leak(double mem_used, double mem_active, double mem_total){
    double used = mem_used / mem_total;
    double act = mem_active / mem_total;
    bool flag = false;

    if(((S_mem_leak_uup > used) || (S_mem_leak_udown < used)) ||
       ((S_mem_leak_aup > act) || (S_mem_leak_adown < act))){
        flag = true;
    }

    return flag;
}

void StaticChecker::GetValue(QVector<double> dvData){
    double cpu_total        = dvData[0];
    double cpu_used         = dvData[1];
    double cpu_user         = dvData[2];
    double cpu_sys          = dvData[3];
    double cpu_iowait       = dvData[4];
    double cpu_idle         = dvData[5];
    double cpu_intr         = dvData[6];
    double cpu_contxt       = dvData[7];
    double cpu_running      = dvData[8];
    double mem_total        = dvData[9];
    double mem_used         = dvData[10];
    double mem_swapin       = dvData[11];
    double mem_swapout      = dvData[12];
    double mem_active       = dvData[13];

    b_cpu_userSys_limit     = CmprS_cpu_userSys(cpu_user, cpu_sys, cpu_total);
    b_cpu_iowait_limit      = CmprS_cpu_iowait(cpu_iowait, cpu_total);
    b_cpu_idleRun_limit     = CmprS_cpu_idleRun(cpu_idle, cpu_total, cpu_running);
    b_cpu_intrUsed_limit    = CmprS_cpu_intrUsed(cpu_intr, cpu_total, cpu_used);
    b_cpu_contxtSys_limit   = CmprS_cpu_contxtSys(cpu_contxt, cpu_total, cpu_sys);
    b_mem_swapinOut_limit   = CmprS_mem_swapinOut(mem_swapin, mem_swapout);
    b_mem_leak_limit        = CmprS_mem_leak(mem_used, mem_active, mem_total);
}

bool StaticChecker::CmprQuota(){
    bool flag = false;
    if( b_cpu_userSys_limit || b_mem_swapinOut_limit ||
        b_cpu_iowait_limit || b_mem_leak_limit ||
        b_cpu_idleRun_limit || b_cpu_intrUsed_limit ||
        b_cpu_contxtSys_limit){
        flag = true;
    }

    return flag;
}

